package com.cooba.activitys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.activitys.user.ChangNumberActivity;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.cooba.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

public class RedWalletRecordActivity extends Activity implements View.OnClickListener {
    private ImageButton acount_record_back_btn;
    private TextView acount_record_detail_tv;
    private TextView acount_balance_tv;
    private RelativeLayout acount_out_record_rl;
    private RelativeLayout acount_out_rl;
    private ImageView acount_draw_out_limit_tip;
    private String money="0.00";
    private String drawMoney="10";
    private PopupWindow tipPopWindow;
    private TextView textView;
    private float max;
    private float acount_balance;
    private String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_wallet_record);
        initView();
        initPopWindow();
        initData();
    }

    private void initPopWindow() {
        View tipview =this.getLayoutInflater().inflate(R.layout.draw_out_tip_dialog, null);
        textView= (TextView) tipview.findViewById(R.id.draw_out_tip_dialog_tv);
        tipPopWindow = new PopupWindow(tipview, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        tipPopWindow.setFocusable(true);
        tipPopWindow.setOutsideTouchable(true);
        tipPopWindow.setBackgroundDrawable(new ColorDrawable());
    }

    private void initData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("user_id", SharedPreferencesUtil.getData(this, "login_id", ""));
        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.acountbalance, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("wei", "获取余额成功" + responseInfo.result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseInfo.result.toString());
                    result = jsonObject.get("status").toString();
                    String msg = jsonObject.get("msg").toString();
                    if (result.equals("1")) {
                        money = jsonObject.get("money").toString();
                        drawMoney = jsonObject.get("drawMoney").toString();
                        acount_balance_tv.setText(money);
                    }else if (result.equals("2")){
                        money = jsonObject.get("money").toString();
                        drawMoney = jsonObject.get("drawMoney").toString();
                        acount_balance_tv.setText(money);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    private void initView() {
        acount_record_back_btn = (ImageButton) findViewById(R.id.acount_record_back_ib);
        acount_record_detail_tv = (TextView) findViewById(R.id.acount_record_detail_tv);
        acount_balance_tv = (TextView) findViewById(R.id.acount_balance_tv);
        acount_out_record_rl = (RelativeLayout) findViewById(R.id.acount_out_record_rl);
        acount_out_rl = (RelativeLayout) findViewById(R.id.acount_out_rl);
        acount_draw_out_limit_tip= (ImageView) findViewById(R.id.acount_draw_out_limit_tip);
        acount_draw_out_limit_tip.setOnClickListener(this);
        acount_record_back_btn.setOnClickListener(this);
        acount_out_rl.setOnClickListener(this);
        acount_out_record_rl.setOnClickListener(this);
        acount_record_detail_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=null;
        String login_id=SharedPreferencesUtil.getData(RedWalletRecordActivity.this,"login_id","");
        switch (v.getId()) {
            case R.id.acount_record_back_ib:
                finish();
                break;
            case R.id.acount_draw_out_limit_tip:
                showPopWindow(v);
                break;
            case R.id.acount_record_detail_tv:
                intent =new Intent(RedWalletRecordActivity.this,RedWalletDetailListActivity.class);
                startActivity(intent);
                break;
            case R.id.acount_out_record_rl:
                intent =new Intent(RedWalletRecordActivity.this,DrawOutRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.acount_out_rl:
                if (TextUtils.isEmpty(drawMoney)) {
                max = Float.parseFloat(drawMoney);
                acount_balance = Float.parseFloat(money);
            }
//                if (acount_balance<max){
//                    String text="余额不足"+drawMoney+"元,无法提现";
//                    Toast.makeText(RedWalletRecordActivity.this,text,Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (!Utils.isnumber(login_id)){
                    showBindDialog();
                    return;
                }
                if (result.equals("1")){
                    intent=new Intent(RedWalletRecordActivity.this,DrawOutActivity.class);
                    intent.putExtra("acount_balance", money);
                    intent.putExtra("drawmoney",drawMoney);
                    //startActivity(intent);
                    startActivityForResult(intent,0);
                }else {
                    Toast.makeText(RedWalletRecordActivity.this,"余额满"+drawMoney+"元,才可以提现",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private void showPopWindow(View v) {
        if (tipPopWindow == null) {
            return;
        }
        if (tipPopWindow.isShowing()) {
            tipPopWindow.dismiss();
        } else {
            String text="余额满"+drawMoney+"元,才可以提现";
            textView.setText(text);
            tipPopWindow.showAsDropDown(v);
        }
    }

    //显示绑定对话框
    public void showBindDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(RedWalletRecordActivity.this);
        builder.setTitle("尚未绑定手机号,是否绑定?");
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(RedWalletRecordActivity.this, ChangNumberActivity.class);
                startActivity(intent);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0){
            initData();
        }
    }
}
